package com.ruoyi.swla2.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DatePattern;
import cn.hutool.json.JSONUtil;
import com.ruoyi.swla2.model.FreezeDataQueryModel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.swla2.domain.DfqFreeze;
import com.ruoyi.swla2.service.IDfqFreezeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大分区冻结数据Controller
 *
 * @author gdqt
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/swla2/freezeDfq")
public class DfqFreezeController extends BaseController {
    @Autowired
    private IDfqFreezeService dfqFreezeService;

    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getDfqProdsaleAnalysisData")
    public AjaxResult getDfqProdsaleAnalysisData(@RequestParam Map<String, Object> paramMap) {
        Map<String, Object> resultMap = new HashMap<>();

        String startDate, endDate;
        Object queryDates = paramMap.get("queryDates");
        if (queryDates == null) {
            startDate = LocalDate.now().minusMonths(6L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN));
            endDate = LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN));
        } else {
            String[] dates = queryDates.toString().split(",");
            startDate = dates[0];
            endDate = dates[1];
        }

        FreezeDataQueryModel model = new FreezeDataQueryModel();
        model.setAreaId(paramMap.get("areaId").toString());
        model.setStartDate(startDate);
        model.setEndDate(endDate);
        model.setFrozenType("month");
        List<DfqFreeze> dfqFreezeList = dfqFreezeService.selectDfqFreezeByQueryModel(model);
        dfqFreezeList.sort(Comparator.comparing(DfqFreeze::getFreezetime));

        resultMap.put("timeList", dfqFreezeList.stream().map(DfqFreeze::getFreezetime).collect(Collectors.toList()));
        resultMap.put("supply", dfqFreezeList.stream().map(item -> item.getInputFlow().doubleValue()).collect(Collectors.toList()));
        resultMap.put("sale", dfqFreezeList.stream().map(item -> item.getSaleWater().doubleValue()).collect(Collectors.toList()));
        resultMap.put("free", dfqFreezeList.stream().map(item -> item.getFreeWater().doubleValue()).collect(Collectors.toList()));
        resultMap.put("cxcl", dfqFreezeList.stream().map(item -> Double.valueOf(item.getCxcl())).collect(Collectors.toList()));

        return AjaxResult.success(resultMap);
    }

    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/getDfqSupplyAnalysisData")
    public AjaxResult getDfqSupplyAnalysisData(@RequestParam Map<String, Object> paramMap) {
        Map<String, Object> resultMap = new HashMap<>();

        String startDate, endDate;
        Object queryDates = paramMap.get("queryDates");
        if (queryDates == null) {
            startDate = LocalDate.now().minusMonths(12L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN));
            endDate = LocalDate.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN));
        } else {
            String[] dates = queryDates.toString().split(",");
            startDate = dates[0];
            endDate = dates[1];
        }

        FreezeDataQueryModel model = new FreezeDataQueryModel();
        model.setAreaId(paramMap.get("areaId").toString());
        model.setStartDate(startDate);
        model.setEndDate(endDate);
        model.setFrozenType("month");
        List<DfqFreeze> dfqFreezeList = dfqFreezeService.selectDfqFreezeByQueryModel(model);
        dfqFreezeList.sort(Comparator.comparing(DfqFreeze::getFreezetime));

        resultMap.put("timeList", dfqFreezeList.stream().map(DfqFreeze::getFreezetime).collect(Collectors.toList()));
        resultMap.put("supply", dfqFreezeList.stream().map(item -> item.getInputFlow().doubleValue()).collect(Collectors.toList()));
        resultMap.put("hb", dfqFreezeList.stream().map(DfqFreeze::getHb).collect(Collectors.toList()));

        return AjaxResult.success(resultMap);
    }

    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/listByQueryModel")
    public TableDataInfo listByQueryModel(FreezeDataQueryModel model) {
        startPage();
        List<DfqFreeze> list = dfqFreezeService.selectDfqFreezeByQueryModel(model);
        return getDataTable(list);
    }

    /**
     * 查询大分区冻结数据列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:list')")
    @GetMapping("/list")
    public TableDataInfo list(DfqFreeze dfqFreeze) {
        startPage();
        List<DfqFreeze> list = dfqFreezeService.selectDfqFreezeList(dfqFreeze);
        return getDataTable(list);
    }

    /**
     * 导出大分区冻结数据列表
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:export')")
    @Log(title = "大分区冻结数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FreezeDataQueryModel model) {
        List<DfqFreeze> list = dfqFreezeService.selectDfqFreezeByQueryModel(model);
        list.forEach(dfqFreeze -> {
            if (dfqFreeze.getZone() != null) {
                dfqFreeze.setAreaName(dfqFreeze.getZone().getAreaName());
            }
        });
        ExcelUtil<DfqFreeze> util = new ExcelUtil<DfqFreeze>(DfqFreeze.class);
        util.exportExcel(response, list, "大分区冻结数据数据");
    }

    /**
     * 获取大分区冻结数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dfqFreezeService.selectDfqFreezeById(id));
    }

    /**
     * 新增大分区冻结数据
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:add')")
    @Log(title = "大分区冻结数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DfqFreeze dfqFreeze) {
        dfqFreeze.setTenantId(this.getTenantId());
        return toAjax(dfqFreezeService.insertDfqFreeze(dfqFreeze));
    }

    /**
     * 修改大分区冻结数据
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:edit')")
    @Log(title = "大分区冻结数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DfqFreeze dfqFreeze) {
        return toAjax(dfqFreezeService.updateDfqFreeze(dfqFreeze));
    }

    /**
     * 删除大分区冻结数据
     */
    @PreAuthorize("@ss.hasPermi('swla2:freezeDfq:remove')")
    @Log(title = "大分区冻结数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(dfqFreezeService.deleteDfqFreezeByIds(ids));
    }


    /* 漏损报表-分区报表 */
    /**
     * 查询大分区冻结数据列表
     */
    @GetMapping("/report/dfq")
    public TableDataInfo listDfq(FreezeDataQueryModel model) {
        model.setTenantId(this.getTenantId());
        startPage();
        List<DfqFreeze> list = dfqFreezeService.selectDfqFreezeReportList(model);
        return getDataTable(list);
    }
}
